package com.xyhuo.leetcode.january;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统计出现过一次的公共字符串
 * https://leetcode.cn/problems/count-common-words-with-one-occurrence/
 *
 * @time 2024/1/12
 */
public class CountWords {

    public int countWords(String[] words1, String[] words2) {
        List<String> word1List = accorTime(words1);
        List<String> word2List = accorTime(words2);
        int ans = 0;
        for (String word1 : word1List) {
            if (word2List.contains(word1)) {
                ans++;
            }
        }
        return ans;
    }

    private List<String> accorTime(String[] words) {
        Map<String, Integer> map = new HashMap<>();
        for (String word : words) {
            int count = 0;
            if (map.containsKey(word)) {
                count = map.get(word);
            }
            map.put(word, count + 1);
        }
        List<String> list = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1) {
                list.add(entry.getKey());
            }
        }
        return list;
    }
}
